package com.whisper.common.utils.file;

import com.whisper.common.config.WhisperConfig;
import com.whisper.common.controller.ProfileResourceController;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.nio.file.FileVisitOption;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 文件下载工具类
 *
 * @author whisper
 */
public class FileDownloadUtils
{
    private static final Logger log = LoggerFactory.getLogger(ProfileResourceController.class);

    /**
     * 默认上传的地址
     */
    private static String defaultBaseDir = WhisperConfig.getProfile();

    public static void setDefaultBaseDir(String defaultBaseDir)
    {
        FileDownloadUtils.defaultBaseDir = defaultBaseDir;
    }

    public static String getDefaultBaseDir()
    {
        return defaultBaseDir;
    }

    public static List<Path> findFilePath(String childPath, int deep, String fileName) {
        if (!childPath.startsWith("/")) childPath = "/" + childPath;
        Path searchPath = Paths.get(defaultBaseDir + childPath);
        try {
            return Files.walk(searchPath, deep, FileVisitOption.FOLLOW_LINKS)
                    .filter(path -> path.getFileName().toString().equals(fileName))
                    .collect(Collectors.toList());
        } catch (IOException e) {
            log.error(e.getMessage());
            throw new RuntimeException(e);
        }
    }

    public static List<Path> findFilePath(String childPath, String fileName) {
        if (!childPath.startsWith("/")) childPath = "/" + childPath;
        Path searchPath = Paths.get(defaultBaseDir + childPath);
        try {
            return Files.walk(searchPath, Integer.MAX_VALUE, FileVisitOption.FOLLOW_LINKS)
                    .filter(path -> path.getFileName().toString().equals(fileName))
                    .collect(Collectors.toList());
        } catch (IOException e) {
            log.error(e.getMessage());
            throw new RuntimeException(e);
        }
    }

    public static List<Path> findFilePath(int deep, String fileName) {
        Path searchPath = Paths.get(defaultBaseDir);
        try {
            return Files.walk(searchPath, deep, FileVisitOption.FOLLOW_LINKS)
                    .filter(path -> path.getFileName().toString().equals(fileName))
                    .collect(Collectors.toList());
        } catch (IOException e) {
            log.error(e.getMessage());
            throw new RuntimeException(e);
        }
    }
    public static List<Path> findFilePath(String fileName) {
        Path searchPath = Paths.get(defaultBaseDir);
        try {
            return Files.walk(searchPath, Integer.MAX_VALUE, FileVisitOption.FOLLOW_LINKS)
                    .filter(path -> path.getFileName().toString().equals(fileName))
                    .collect(Collectors.toList());
        } catch (IOException e) {
            log.error(e.getMessage());
            throw new RuntimeException(e);
        }
    }
}
